<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="全局XML配置文件MyBatis配置文件的结构如下：  configuration 配置 properties 属性 settings 参数设置 typeAliases 类型别名 typeHandlers 类型处理器 objectFactory 对象工厂 plugins 插件 environments 环境集合 environment 环境 transactionManager 事务管理器 da">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis-XML配置与映射文件">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/06/11/2016-06-11-MyBatis-XML%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="全局XML配置文件MyBatis配置文件的结构如下：  configuration 配置 properties 属性 settings 参数设置 typeAliases 类型别名 typeHandlers 类型处理器 objectFactory 对象工厂 plugins 插件 environments 环境集合 environment 环境 transactionManager 事务管理器 da">
<meta property="og:locale">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f4pf96s0tcj209302caa2.jpg">
<meta property="article:published_time" content="2016-06-10T16:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="MyBatis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f4pf96s0tcj209302caa2.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/06/11/2016-06-11-MyBatis-XML%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>MyBatis-XML配置与映射文件 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%A8%E5%B1%80XML%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">全局XML配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E6%A0%87%E7%AD%BE%E9%85%8D%E7%BD%AE"><span class="nav-number">1.1.</span> <span class="nav-text">常用标签配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-properties"><span class="nav-number">1.1.1.</span> <span class="nav-text">1. properties</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-settings"><span class="nav-number">1.1.2.</span> <span class="nav-text">2. settings</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-typeAliases"><span class="nav-number">1.1.3.</span> <span class="nav-text">3. typeAliases</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-Mappers"><span class="nav-number">1.1.4.</span> <span class="nav-text">4. Mappers</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mapper%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6"><span class="nav-number">2.</span> <span class="nav-text">Mapper映射文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#resultType"><span class="nav-number">2.1.</span> <span class="nav-text">resultType</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#resultMap"><span class="nav-number">2.2.</span> <span class="nav-text">resultMap</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E6%80%81SQL"><span class="nav-number">2.3.</span> <span class="nav-text">动态SQL</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-SQL%E7%89%87%E6%AE%B5"><span class="nav-number">2.3.1.</span> <span class="nav-text">1. SQL片段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-if%E6%A0%87%E7%AD%BE"><span class="nav-number">2.3.2.</span> <span class="nav-text">2. if标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-where%E6%A0%87%E7%AD%BE"><span class="nav-number">2.3.3.</span> <span class="nav-text">3. where标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-foreach%E6%A0%87%E7%AD%BE"><span class="nav-number">2.3.4.</span> <span class="nav-text">4. foreach标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-bind%E6%A0%87%E7%AD%BE"><span class="nav-number">2.3.5.</span> <span class="nav-text">5. bind标签</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/06/11/2016-06-11-MyBatis-XML%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MyBatis-XML配置与映射文件
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-06-11 00:00:00" itemprop="dateCreated datePublished" datetime="2016-06-11T00:00:00+08:00">2016-06-11</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/MyBatis/" itemprop="url" rel="index"><span itemprop="name">MyBatis</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f4pf96s0tcj209302caa2.jpg" alt="MyBatis"></p>
<h2 id="全局XML配置文件"><a href="#全局XML配置文件" class="headerlink" title="全局XML配置文件"></a>全局XML配置文件</h2><p><code>MyBatis</code>配置文件的结构如下：</p>
<ul>
<li>configuration 配置<ul>
<li>properties 属性</li>
<li>settings 参数设置</li>
<li>typeAliases 类型别名</li>
<li>typeHandlers 类型处理器</li>
<li>objectFactory 对象工厂</li>
<li>plugins 插件</li>
<li>environments 环境集合<ul>
<li>environment 环境<ul>
<li>transactionManager 事务管理器</li>
<li>dataSource 数据源</li>
</ul>
</li>
<li>databaseIdProvider 数据库厂商标识</li>
<li>mappers 映射器</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="常用标签配置"><a href="#常用标签配置" class="headerlink" title="常用标签配置"></a>常用标签配置</h3><h4 id="1-properties"><a href="#1-properties" class="headerlink" title="1. properties"></a>1. <strong>properties</strong></h4><pre><code>properties标签可以读取properties配置文件,并引入到 MyBatis 的配置文件中。
可以通过子标签定义key/value。</code></pre>
<p>例如：</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;properties resource=<span class="string">&quot;com/sun/test/dataSource.properties&quot;</span>&gt;</span><br><span class="line">    &lt;property name=<span class="string">&quot;username&quot;</span> value=<span class="string">&quot;dev_user&quot;</span>/&gt;</span><br><span class="line">    &lt;property name=<span class="string">&quot;password&quot;</span> value=<span class="string">&quot;F2Fa3!33TYyg&quot;</span>/&gt;</span><br><span class="line">&lt;/properties&gt;</span><br></pre></td></tr></table></figure>
<p> 其中的属性就可以在整个配置文件中使用key来替换需要动态配置的属性值。</p>
<p>例如：</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;dataSource type=<span class="string">&quot;POOLED&quot;</span>&gt;</span><br><span class="line">  &lt;property name=<span class="string">&quot;driver&quot;</span> value=<span class="string">&quot;$&#123;driver&#125;&quot;</span>/&gt;</span><br><span class="line">  &lt;property name=<span class="string">&quot;url&quot;</span> value=<span class="string">&quot;$&#123;url&#125;&quot;</span>/&gt;</span><br><span class="line">  &lt;property name=<span class="string">&quot;username&quot;</span> value=<span class="string">&quot;$&#123;username&#125;&quot;</span>/&gt;</span><br><span class="line">  &lt;property name=<span class="string">&quot;password&quot;</span> value=<span class="string">&quot;$&#123;password&#125;&quot;</span>/&gt;</span><br><span class="line">&lt;/dataSource&gt;</span><br></pre></td></tr></table></figure>

<p>properties的加载顺序</p>
<ul>
<li>最先加载properties中的子标签所定义的属性。</li>
<li>然后会加载resource标签所引入的properties配置文件中的属性,并覆盖已加载的同名属性。</li>
<li>最后加载作为方法参数(parameterType中的key/value)传递的属性，并覆盖已加载的同名属性。</li>
</ul>
<p>总结：</p>
<ul>
<li>通过方法参数传递的属性具有最高优先级。</li>
<li>resource引用的配置文件中的属性次之。</li>
<li>最低优先级的是 properties 子标签中指定的属性。</li>
</ul>
<h4 id="2-settings"><a href="#2-settings" class="headerlink" title="2. settings"></a>2. <strong>settings</strong></h4><pre><code>settings会影响MyBatis的运行行为。</code></pre>
<p>一个配置完整的 settings 的示例如下：</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;settings&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;cacheEnabled&quot;</span> value=<span class="string">&quot;true&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;lazyLoadingEnabled&quot;</span> value=<span class="string">&quot;true&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;multipleResultSetsEnabled&quot;</span> value=<span class="string">&quot;true&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;useColumnLabel&quot;</span> value=<span class="string">&quot;true&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;useGeneratedKeys&quot;</span> value=<span class="string">&quot;false&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;autoMappingBehavior&quot;</span> value=<span class="string">&quot;PARTIAL&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;autoMappingUnknownColumnBehavior&quot;</span> value=<span class="string">&quot;WARNING&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;defaultExecutorType&quot;</span> value=<span class="string">&quot;SIMPLE&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;defaultStatementTimeout&quot;</span> value=<span class="string">&quot;25&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;defaultFetchSize&quot;</span> value=<span class="string">&quot;100&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;safeRowBoundsEnabled&quot;</span> value=<span class="string">&quot;false&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;mapUnderscoreToCamelCase&quot;</span> value=<span class="string">&quot;false&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;localCacheScope&quot;</span> value=<span class="string">&quot;SESSION&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;jdbcTypeForNull&quot;</span> value=<span class="string">&quot;OTHER&quot;</span>/&gt;</span><br><span class="line">  &lt;setting name=<span class="string">&quot;lazyLoadTriggerMethods&quot;</span> value=<span class="string">&quot;equals,clone,hashCode,toString&quot;</span>/&gt;</span><br><span class="line">&lt;/settings&gt;</span><br></pre></td></tr></table></figure>
<h4 id="3-typeAliases"><a href="#3-typeAliases" class="headerlink" title="3. typeAliases"></a>3. <strong>typeAliases</strong></h4><p>类型别名,可以对JavaBean设置一个别名,用来减少完全限定名的冗余。<br>例如：</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 类型别名 --&gt;</span><br><span class="line">	&lt;typeAliases&gt;</span><br><span class="line">		&lt;!-- 设置单个别名 --&gt;</span><br><span class="line">		&lt;typeAlias type=<span class="string">&quot;com.sun.test.entity.User&quot;</span> alias=<span class="string">&quot;user&quot;</span>/&gt;</span><br><span class="line">		&lt;!-- package指定一个包名,会在包下搜索需要的JavaBean</span><br><span class="line">			   如果没有注解,则默认设置为JavaBean的首字母小写的非限定类名作为它的别名。</span><br><span class="line">			   如果有@Alias注解,则使用其注解值作为它的别名。</span><br><span class="line">		 --&gt;</span><br><span class="line">		 &lt;package name=<span class="string">&quot;com.sun.test.entity&quot;</span>/&gt;</span><br><span class="line">	&lt;/typeAliases&gt;</span><br></pre></td></tr></table></figure>
<p><code>MyBatis</code>默认支持的类型别名如下,它们都是大小写不敏感的，需要注意的是由基本类型名称重复导致的特殊处理。</p>
<table>
<thead>
<tr>
<th>别名</th>
<th>映射的类型</th>
</tr>
</thead>
<tbody><tr>
<td>_byte</td>
<td>byte</td>
</tr>
<tr>
<td>_long</td>
<td>long</td>
</tr>
<tr>
<td>_short</td>
<td>short</td>
</tr>
<tr>
<td>_int</td>
<td>int</td>
</tr>
<tr>
<td>_integer</td>
<td>int</td>
</tr>
<tr>
<td>_double</td>
<td>double</td>
</tr>
<tr>
<td>_float</td>
<td>float</td>
</tr>
<tr>
<td>_boolean</td>
<td>boolean</td>
</tr>
<tr>
<td>string</td>
<td>String</td>
</tr>
<tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>integer</td>
<td>Integer</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
</tr>
<tr>
<td>date</td>
<td>Date</td>
</tr>
<tr>
<td>decimal</td>
<td>BigDecimal</td>
</tr>
<tr>
<td>bigdecimal</td>
<td>BigDecimal</td>
</tr>
<tr>
<td>object</td>
<td>Object</td>
</tr>
<tr>
<td>map</td>
<td>Map</td>
</tr>
<tr>
<td>hashmap</td>
<td>HashMap</td>
</tr>
<tr>
<td>list</td>
<td>List</td>
</tr>
<tr>
<td>arraylist</td>
<td>ArrayList</td>
</tr>
<tr>
<td>collection</td>
<td>Collection</td>
</tr>
<tr>
<td>iterator</td>
<td>Iterator</td>
</tr>
</tbody></table>
<h4 id="4-Mappers"><a href="#4-Mappers" class="headerlink" title="4. Mappers"></a>4. <strong>Mappers</strong></h4><p>映射器(Mappers)用于引入Mapper映射文件。</p>
<p>使用相对路径的方式引入</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 相对路径 --&gt;</span><br><span class="line">&lt;mappers&gt;</span><br><span class="line">  &lt;mapper resource=<span class="string">&quot;com/sun/test/mapper/UserMapper.xml&quot;</span>/&gt;</span><br><span class="line">&lt;/mappers&gt;</span><br></pre></td></tr></table></figure>
<p>使用绝对路径的方式引入</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 绝对路径s --&gt;</span><br><span class="line">&lt;mappers&gt;</span><br><span class="line">  &lt;mapper url=<span class="string">&quot;file:///D:\sun\mappers\UserMapper.xml&quot;</span>/&gt;</span><br><span class="line">&lt;/mappers&gt;</span><br></pre></td></tr></table></figure>
<p>使用Mapper接口的全限定类名(需要mapper接口与mapper映射文件名称相同,并且在同一个包下)</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Mapper接口的全限定类名 --&gt;</span><br><span class="line">&lt;mappers&gt;</span><br><span class="line">  &lt;mapper class=<span class="string">&quot;com.sun.test.mapper.UserMapper&quot;</span>/&gt;</span><br><span class="line">&lt;/mappers&gt;</span><br></pre></td></tr></table></figure>
<p>扫描指定包下的所有映射文件(需要mapper接口与mapper映射文件名称相同,并且在同一个包下)</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 扫描指定包下的所有映射文件 --&gt;</span><br><span class="line">&lt;mappers&gt;</span><br><span class="line">  &lt;package name=<span class="string">&quot;com.sun.test.mapper&quot;</span>/&gt;</span><br><span class="line">&lt;/mappers&gt;</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="Mapper映射文件"><a href="#Mapper映射文件" class="headerlink" title="Mapper映射文件"></a>Mapper映射文件</h2><ul>
<li><code>cache</code> 缓存配置</li>
<li><code>cache-ref</code> 其他namespace缓存配置的引用</li>
<li><code>resultType</code> 结果集映射的类型</li>
<li><code>resultMap</code> 结果集映射Map</li>
<li><code>parameterType</code> 输入映射类型</li>
<li><code>sql</code> 可被引用的复用sql语句块</li>
<li><code>insert</code> 映射插入语句</li>
<li><code>update</code> 映射更新语句</li>
<li><code>select</code> 映射查询语句</li>
<li><code>delete</code> 映射删除语句</li>
</ul>
<h3 id="resultType"><a href="#resultType" class="headerlink" title="resultType"></a>resultType</h3><p>注意事项:</p>
<ol>
<li>如果使用resultType进行结果集映射,则需要查询出的列名与映射的JavaBean属性名称一致。</li>
<li>SQL语句中列名如果有别名,则列名为别名的名称。</li>
<li>如果查询的列名和JavaBean所映射的属性名全不一致,则映射的JavaBean对象为null。</li>
<li>如果查询的列名和JavaBean所映射的属性名少数不一致,则映射的JavaBean对象不为null,但只有名称一致的属性有值。</li>
</ol>
<p>例如：</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">   &lt;!-- 根据id查询User --&gt;</span><br><span class="line">&lt;select id=<span class="string">&quot;findById&quot;</span> parameterType=<span class="string">&quot;int&quot;</span> resultType=<span class="string">&quot;com.sun.test.entity.User&quot;</span>&gt;</span><br><span class="line">	select * from user where id = #&#123;id&#125;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<h3 id="resultMap"><a href="#resultMap" class="headerlink" title="resultMap"></a>resultMap</h3><p>注意事项:</p>
<ol>
<li>使用resultMap进行结果集映射,不需要列名与映射的JavaBean属性名称一致。</li>
<li>使用resultMap进行结果集映射,需要先定义一个resultMap。</li>
</ol>
<p>例如:</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 定义一个resultMap --&gt;</span><br><span class="line">&lt;resultMap type=<span class="string">&quot;com.sun.test.entity.User&quot;</span> id=<span class="string">&quot;userResultMap&quot;</span>&gt;</span><br><span class="line">	&lt;!-- 主键 --&gt;</span><br><span class="line">	&lt;id column=<span class="string">&quot;id&quot;</span> property=<span class="string">&quot;id&quot;</span>/&gt;</span><br><span class="line">	&lt;!-- 其他字段 --&gt;</span><br><span class="line">	&lt;result column=<span class="string">&quot;username&quot;</span> property=<span class="string">&quot;username&quot;</span>/&gt;</span><br><span class="line">&lt;/resultMap&gt;</span><br><span class="line">&lt;select id=<span class="string">&quot;findByIdWithRetMap&quot;</span> parameterType=<span class="string">&quot;int&quot;</span> resultMap=<span class="string">&quot;userResultMap&quot;</span>&gt;</span><br><span class="line">	select id,username from user where id = #&#123;id&#125;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<h3 id="动态SQL"><a href="#动态SQL" class="headerlink" title="动态SQL"></a>动态SQL</h3><h4 id="1-SQL片段"><a href="#1-SQL片段" class="headerlink" title="1. SQL片段"></a>1. SQL片段</h4><p>sql片段可以存储动态的sql语句,提高复用性。</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- SQL片段,可以存储动态的SQL语句 --&gt;</span><br><span class="line">	&lt;sql id=<span class="string">&quot;sql&quot;</span>&gt;</span><br><span class="line">		&lt;!-- 判断用户名不为空 --&gt;</span><br><span class="line">		&lt;<span class="keyword">if</span> test=<span class="string">&quot;username != null and username != &#x27;&#x27;&quot;</span>&gt;</span><br><span class="line">			and username = #&#123;usernmae&#125;</span><br><span class="line">		&lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">		&lt;!-- 判断性别不为空 --&gt;</span><br><span class="line">		&lt;<span class="keyword">if</span> test=<span class="string">&quot;sex != null and sex != &#x27;&#x27;&quot;</span>&gt;</span><br><span class="line">			and sex = #&#123;sex&#125;</span><br><span class="line">		&lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">	&lt;/sql&gt;</span><br></pre></td></tr></table></figure>
<h4 id="2-if标签"><a href="#2-if标签" class="headerlink" title="2. if标签"></a>2. if标签</h4><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=<span class="string">&quot;findByIdOrLikeName&quot;</span> resultType=<span class="string">&quot;com.sun.test.entity.User&quot;</span>&gt;</span><br><span class="line">  select * from user where id = #&#123;id&#125;</span><br><span class="line">  &lt;!-- 如果<span class="keyword">if</span>返回为true 则会加上这条语句 --&gt;</span><br><span class="line">  &lt;<span class="keyword">if</span> test=<span class="string">&quot;username != null and username != &#x27;&#x27;&quot;</span>&gt;</span><br><span class="line">    and username like <span class="string">&#x27;%$&#123;username&#125;%&#x27;</span></span><br><span class="line">  &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<h4 id="3-where标签"><a href="#3-where标签" class="headerlink" title="3. where标签"></a>3. where标签</h4><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=<span class="string">&quot;findAllOrLikeName&quot;</span> resultType=<span class="string">&quot;com.sun.test.entity.User&quot;</span>&gt;</span><br><span class="line">  select * from user</span><br><span class="line">  &lt;!-- where标签只有在<span class="keyword">if</span>为true的情况下才会添加where语句</span><br><span class="line">       并且会将第一条语句的and去掉。</span><br><span class="line">  --&gt;</span><br><span class="line">  &lt;where&gt;</span><br><span class="line">    &lt;<span class="keyword">if</span> test=<span class="string">&quot;username != null and username != &#x27;&#x27;&quot;</span>&gt;</span><br><span class="line">        and username like <span class="string">&#x27;%$&#123;username&#125;%&#x27;</span></span><br><span class="line">     &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">  &lt;/where&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<h4 id="4-foreach标签"><a href="#4-foreach标签" class="headerlink" title="4. foreach标签"></a>4. foreach标签</h4><p>foreach标签可以对一个集合进行遍历,通常是在构建一个in条件语句的时候使用。</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=<span class="string">&quot;selectUserIn&quot;</span> resultType=<span class="string">&quot;com.sun.test.entity.User&quot;</span>  parameterType=<span class="string">&quot;list&quot;</span>&gt;</span><br><span class="line">	select * from user</span><br><span class="line">	&lt;where&gt;</span><br><span class="line">		&lt;<span class="keyword">if</span> test=<span class="string">&quot;list != null and list.size &gt; 0&quot;</span>&gt;</span><br><span class="line">			&lt;!-- </span><br><span class="line">				collection：集合参数的名称。</span><br><span class="line">				item：遍历集合时的值</span><br><span class="line">				open：遍历开始时需要拼接的sql语句</span><br><span class="line">				close：遍历结束时需要拼接的sql语句</span><br><span class="line">				separator：遍历过程中需要拼接的字符串</span><br><span class="line">			 --&gt;</span><br><span class="line">			&lt;foreach collection=<span class="string">&quot;list&quot;</span> item=<span class="string">&quot;id&quot;</span> open=<span class="string">&quot;and id in(&quot;</span> close=<span class="string">&quot;)&quot;</span> separator=<span class="string">&quot;,&quot;</span>&gt;</span><br><span class="line">				#&#123;id&#125;</span><br><span class="line">			&lt;/foreach&gt;</span><br><span class="line">		&lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">	&lt;/where&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<h4 id="5-bind标签"><a href="#5-bind标签" class="headerlink" title="5. bind标签"></a>5. bind标签</h4><p>bind标签可以从OGNL表达式中创建一个变量并将其绑定到上下文。</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=<span class="string">&quot;selectUserLikeName&quot;</span> resultType=<span class="string">&quot;com.sun.test.entity.User&quot;</span>&gt;</span><br><span class="line">  &lt;bind name=<span class="string">&quot;username&quot;</span> value=<span class="string">&quot;&#x27;%&#x27; + user.getUsername() + &#x27;%&#x27;&quot;</span> /&gt;</span><br><span class="line">  select * from user</span><br><span class="line">  where username like #&#123;username&#125;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/MyBatis/" rel="tag"># MyBatis</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/06/10/2016-06-10-mybatis-study%20notes01/" rel="prev" title="MyBatis学习笔记-01">
                  <i class="fa fa-chevron-left"></i> MyBatis学习笔记-01
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/06/12/2016-06-12-mybatis-study%20notes02/" rel="next" title="MyBatis学习笔记-02">
                  MyBatis学习笔记-02 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
